const db = wx.cloud.database();
const musiclists = db.collection('musicList');
/**实现播放器播放功能 */
const audioCtx = wx.createInnerAudioContext()

Page({
  data: {
    /**轮播图 */
    swiper: ["https://img-qn-3.51miz.com/preview/element/00/01/22/17/E-1221756-30E61033.jpg!/quality/90/unsharp/true/compress/true/format/jpg/fh/400", "https://img-qn-4.51miz.com/preview/element/00/01/22/36/E-1223618-CB561756.jpg!/quality/90/unsharp/true/compress/true/format/jpg/fh/400"],
    grid: [{ title: "私人FM", icon: "photo-o", click: "persion" }, { title: "每日歌曲推荐", icon: "music-o", click: "everyDay" }, { title: "云音乐新歌榜", icon: "bar-chart-o", click: "yun" }],
    state: false,
    playIndex: 0,
    musiclist: [],
    play: {
      currentTime: 0,
      duration: 0,
      percent: 0,
      title: '',
      singer: '',
      coverImg: '',
    }
  },
  onLoad() {
    musiclists.get().then(res => {
      this.setData({
        musiclist: res.data.sort(sortNumber)
      })
      //console.log(this.data.musiclist)
      this.selectMusic(0)
    })
    audioCtx.onError(() => {
      console.log('播放失败：' + audioCtx.src)
    })
    audioCtx.onEnded(() => {
      this.next()
    })
    audioCtx.onCanplay(() => {
      //console.log('onCanplay')
      this.setData({
        'play.duration': audioCtx.duration
      })
    })
    audioCtx.onTimeUpdate(() => {
      this.setData({
        'play.duration': audioCtx.duration,
        'play.currentTime': audioCtx.currentTime,
        'play.percent': ((audioCtx.currentTime / audioCtx.duration * 100))
      });
    })
  },

  onClickLeft() {
    wx.navigateBack({
      delta: 1,
    })
  },

  /**切换音乐 */
  selectMusic(index) {
    var music = this.data.musiclist[index]
    audioCtx.src = music.src
    //console.log("切换音乐")
    //console.log(music)
    this.setData({
      state: true,
      playIndex: index,
      play: music,
      'play.currentTime': 0,
      'play.duration': 0,
      'play.percent': 0,
    });
    audioCtx.play()
  },

  //下一曲按钮
  next() {
    var index = this.data.playIndex >= this.data.musiclist.length - 1 ? 0 : this.data.playIndex + 1
    this.selectMusic(index);
  },

  //上一曲按钮
  prev() {
    var index = this.data.playIndex <= 0 ? this.data.musiclist.length - 1 : this.data.playIndex - 1
    this.selectMusic(index);
  },

  //播放列表换曲功能
  // change(e) {
  //   this.selectMusic(e.currentTarget.dataset.index)
  //   this.play()
  // },
  /**grid点击事件*/
  persion() {
    console.log("persion")
  },
  everyDay() {
    console.log("每日")
  },
  yun() {
    console.log("云")
  },

  /**滑动条改变事件 */
  sliderChange(e) {
    audioCtx.seek(e.detail.value * (this.data.play.duration / 100));
  },

  /**推荐歌曲点击事件 */
  musiclistClick(e) {
    //console.log(e)
    let index = e.currentTarget.dataset.id;
    audioCtx.src = this.data.musiclist[index].src;
    this.selectMusic(index)
  },

  play_icon() {
    this.setData({
      state: !this.data.state
    });
    if (this.data.state) {
      audioCtx.play()
    } else {
      audioCtx.pause()
    };
  }
})

/**打乱数组 */
function sortNumber(a, b) {
  // 利用随机数 当随机数大于0.5 返回-1
  return Math.random() > .5 ? -1 : 1;
}